package com.mask.roll.edu.controller;


import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.exception.RollException;
import com.mask.roll.common.util.RollResponse;
import com.mask.roll.edu.client.VodClient;
import com.mask.roll.edu.entity.Video;
import com.mask.roll.edu.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author hx
 * @since 2021-10-23
 */
@RestController
@RequestMapping("/edu/video")
@Api(tags = "课程小节管理")
@Slf4j
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private VodClient vodClient;

    @PostMapping("/add")
    @ApiOperation("添加小节")
    public RollResponse addVideo(@RequestBody Video video){
        videoService.save(video);
        return RollResponse.result(ServiceEnums.VIDEO_ADD_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.VIDEO_ADD_SUCCESS.getMessage());
    }

    /**
     * 删除小节
     * TODO 删除小节后的视频 Nacos
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除小节")
    public RollResponse deleteVideo(@ApiParam("小节id") @PathVariable String id){

        //获取视频id
        Video video = videoService.getById(id);
        String videoSourceId = video.getVideoSourceId();

        if (!StringUtils.isEmpty(videoSourceId)) {
            //远程调用vod删除接口
            vodClient.deleteVod(videoSourceId);
//            try {
//
//            } catch (Exception e) {
//                log.error("删除视频失败:{}",e.getMessage());
//                throw new RollException(ServiceEnums.VOD_DELETE_FAILED.getCode(),
//                        RollConstants.ERROR,ServiceEnums.VOD_DELETE_FAILED.getMessage());
//            }
        }

        videoService.removeById(id);

        return RollResponse.result(ServiceEnums.VIDEO_DELETE_SUCCESS.getCode(),
                RollConstants.SUCCESS,ServiceEnums.VIDEO_DELETE_SUCCESS.getMessage());

    }

    //TODO 修改小节

}

